package com.ylbh.modules.version.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.version.dto.AddAppVersionDTO;
import com.ylbh.common.pojo.version.dto.AppVersionDTO;
import com.ylbh.common.pojo.version.vo.VersionVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.version.entity.ShoppingAndroidApp;
import com.ylbh.modules.version.service.ShoppingAndroidAppService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;


/**
 *
 * app版本控制
 * @author czx
 * @date 2019-04-29 14:46:58
 */
@RestController
@RequestMapping("app/version")
public class ShoppingAndroidAppController {

    @Autowired
    private ShoppingAndroidAppService shoppingAndroidappService;

    @Value("${url.apkFile}")
    private String apkFile;

    @ApiOperation("获取app版本控制列表信息")
    @RequestMapping("queryVersionList")
    @RequiresPermissions("app:version:queryVersionList")
    public CommonResult<PageUtils> queryVersionList(AppVersionDTO appVersionDTO) {
        PageUtils list = shoppingAndroidappService.queryVersionList(appVersionDTO);
        return Ref.ok(list);
    }

    @ApiOperation("获取app版本控制详情信息")
    @RequestMapping("getVersionById/{id}")
    @RequiresPermissions("app:version:getVersionById")
    public CommonResult<ShoppingAndroidApp> getVersionById(@PathVariable(value = "id")Long id) {
        ShoppingAndroidApp app = shoppingAndroidappService.getVersionById(id);
        return Ref.ok(app);
    }

    @ApiOperation("上传apk")
    @RequestMapping("addApk")
    public CommonResult<String> addApk(MultipartFile file, HttpServletRequest request){
        String newMasterVer = request.getParameter("newMasterVer");
        if (StringUtils.isEmpty(newMasterVer)) {
            return Ref.error("文件上传失败！");
        }
        try {
            InputStream inputStream = file.getInputStream();
            OSSClientUtil oss = new OSSClientUtil();
            String apk = oss.uploadOssApkFile(inputStream, "ylbh_" +newMasterVer + ".apk", apkFile);
            return Ref.ok("https://file.yilianbaihui.cn/" +apkFile + "/"+apk);
        }catch (Exception e){
            e.printStackTrace();
            return Ref.error("文件上传失败！");
        }

    }

    @ApiOperation("新的版本号")
    @RequestMapping("getVersionByType/{appType}")
    @RequiresPermissions("app:version:getVersionByType")
    public CommonResult<VersionVO> getVersionByType(@PathVariable(value = "appType") Long appType) {
        VersionVO vo = shoppingAndroidappService.getVersionByType(appType);
        return Ref.ok(vo);
    }

    @SysLog("新增版本号")
    @ApiOperation("新增版本号")
    @RequestMapping("addAppVersion")
    @RequiresPermissions("app:version:addAppVersion")
    public CommonResult<Void> addAppVersion(@RequestBody AddAppVersionDTO addAppVersionDTO) {
        shoppingAndroidappService.addAppVersion(addAppVersionDTO);
        return Ref.ok();
    }

}
